<template>
  <div>
    
 
    <el-dialog :title="form._id ? '修改' : '新增'" :visible.sync="dialogFormVisible" width="400px">
      <el-form :model="form" ref="form" :rules="rules" label-width="100px">
        <el-form-item label="图片" prop="img">
          <div @click="openFile" class="jh">
          <img style="max-width:100%;max-height:100%" v-if="form.img" :src="form.img" />
          <span v-else>+</span>
        </div>
        <input ref="file" style="display:none;" type="file" multiple accept="image/*" @change="changeFile">
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input type="text" v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="跳转地址" prop="url">
          <el-input type="text" v-model="form.url"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" prop="status">
          <el-input type="text" v-model="form.status"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择">
            <el-option
              v-for="type in types"
              :key="type.value"
              :label="type.label"
              :value="type.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>



<script>
import api from '@/api'
export default {
  name: "shouyeBannerEdit",
  components: {},
  data() {
    return {
      dialogFormVisible: false,
      form: {
        _id:0,
        img: "",
        url: "",
        title: "",
      },
      rules: {
        img: [{ required: true, message: "图片不能为空", trigger: "blur" }],
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
      
      },
      types: [
        { label: "轮播图", value: "banner" },
        { label: "分类区", value: "category" },
        { label: "为你推荐", value: "recommend" },
      ],

    };
  },
  methods: {
        openFile(){
      // let fileElement = document.getElementById('file')//id="file"
      let fileElement = this.$refs.file//ref="file"
      fileElement.click()
    },
    //选择上传文件
    changeFile(e){
      let files = e.target.files
      let formData = new FormData()
      formData.append('file',files[0])

      this.$http({
        url:'/upload',
        method:'POST',
        data:formData
        
      }).then(res => {
        if(res.path) this.form.img = api.baseURL + '/' + res.path.slice(0,7) + '/' + res.path.slice(8)
      })
      
    },
    //编辑
    Edit(row) {
      this.form = row
      this.dialogFormVisible = true;
    },
 
    add() {
      //初始化
       this.form= {
         type: "banner",
        _id:0,
        img: '',
        url: '',
        title: '',
      }
      this.dialogFormVisible = true;
    },
    //新增提交
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.form.indexId = this.form._id
          this.$http({
            url: "/admin/index",
            method: this.form.indexId ? 'PUT' : 'POST',//根据indexId判断是新增还是修改
            data: this.form,
          }).then((res) => {
            if (!res.errorMsg) {
              this.dialogFormVisible = false;
              this.$emit('load',this.form.indexId ? undefined : 1)
            }
          });
        }
      });
    },

  },
  created() {
  },
  mounted() {},
};
</script>

<style>
.top {
  display: flex;
  width: 90%;

  margin-left: 50px;
  margin-top: 20px;
  background: #fff;
  /* background: red; */
}
.rqk {
  margin-left: 20px;
}
.jh{
  overflow: hidden;
  width:100px;
  height:100px;
  font-size:32px;
  border:1px dashed #ccc;
  line-height:100px;
  text-align: center;
}
.bg {
  width: 90%;
  margin-top: 10px;
  margin-left: 50px;
}
.button {
  width: 90%;
  margin-top: 20px;
  margin-left: 50px;
}
</style>
